﻿using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StorageManage.ExportModel
{
    public partial class ExportStorageOut : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.QueryString["searchData"] != null)
            {
                string orgSearchData = Request.QueryString["searchData"];
                string searchData = "";
                if (orgSearchData.StartsWith("["))
                {
                    searchData = Request.QueryString["searchData"].Trim('"');
                }
                else
                {
                    searchData = Request.QueryString["searchData"].Remove(0, 1).Trim('"');
                }

                string search = searchData.Replace("\\", "");
                listSearch = ModelTools.GetSearchContions(search);

            }


            Expression<Func<View_StorgaeOut, bool>> exp = SearchExpression.FindByGroup<View_StorgaeOut>(listSearch);
            var list = DALSession.Current.View_StorgaeOutDAL.GetListBy(exp);



            using (FileStream fs = File.OpenRead(System.Web.HttpContext.Current.Server.MapPath("/Content/Excel/余料出库模板.xls")))
            {
                IWorkbook wk = new HSSFWorkbook(fs);

                ISheet sheet0 = wk.GetSheetAt(0);
                ICellStyle cellStyle = wk.CreateCellStyle();//单元格的格式
                cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
                cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
                cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
                cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
                cellStyle.Alignment = HorizontalAlignment.Center;
                cellStyle.VerticalAlignment = VerticalAlignment.Center;
                //cellStyle.WrapText = true;
                IFont font1 = wk.CreateFont();
                font1.FontHeightInPoints = 11;
                font1.FontName = "宋体";
                cellStyle.SetFont(font1);

                int h = 0;
                list.ForEach(o =>
                {
                    IRow row = sheet0.CreateRow(h + 1);
                    row.HeightInPoints = 15;
                    row.CreateCell(0).SetCellValue(o.OutCode);
                    row.CreateCell(1).SetCellValue(o.OutTeam);
                    row.CreateCell(2).SetCellValue(o.TaskCode);
                    row.CreateCell(3).SetCellValue(o.ProjName);
                    row.CreateCell(4).SetCellValue(o.EngName);
                    row.CreateCell(5).SetCellValue(o.HmCode);
                    row.CreateCell(6).SetCellValue(o.MaterialCode);
                    row.CreateCell(7).SetCellValue(o.Name);
                    row.CreateCell(8).SetCellValue(o.Guige);
                    row.CreateCell(9).SetCellValue(o.Caizhi);
                    row.CreateCell(10).SetCellValue(o.PurcUnit);
                    row.CreateCell(11).SetCellValue(Convert.ToDouble(o.OutZhangNum));
                    row.CreateCell(12).SetCellValue(Convert.ToDouble(o.OutTotalWeight));
                    row.CreateCell(13).SetCellValue(o.Note);
                    row.CreateCell(14).SetCellValue(Convert.ToDouble(o.Length));
                    row.CreateCell(15).SetCellValue(Convert.ToDouble(o.Width));
                    row.CreateCell(16).SetCellValue(o.MaterialType);
                    row.CreateCell(17).SetCellValue(o.OutPerson);
                    row.CreateCell(18).SetCellValue(o.ApplyPerson);
                    row.CreateCell(19).SetCellValue(o.ApplyTime);
                    for (int i = 0; i < 20; i++)
                    {
                        row.GetCell(i).CellStyle = cellStyle;
                    }
                    h++;
                });
                MemoryStream file = new MemoryStream();

                wk.Write(file);
                Response.AddHeader("Content-Disposition", string.Format("attachment;filename=余料出库" + DateTime.Now.ToString("yyyyMMdd") + ".xls"));
                HttpContext.Current.Response.BinaryWrite(file.GetBuffer());
                Response.Flush();
                HttpContext.Current.Response.End();

            }
        }
    }
}